package com.service.http.config;

import com.service.http.httpclient.HttpClientFactory;
import com.service.http.okhttp.OkHttpFactory;
import com.service.http.ssl.SSLClientHttpRequestFactory;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import org.apache.http.client.HttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * 描述：
 * 接口调用api
 * @author xianggj
 * @Date 2021/8/30 13:43
 **/
@Slf4j
@PropertySource(value = {"classpath:http.properties"}, encoding = "utf-8", ignoreResourceNotFound=true)
public class HttpConfig {

    @Value("${spring.http.skip.ssl}")
    private Boolean freeSsl;

    @Value("${spring.http.read.timeout:60000}")
    private int readTimeout;

    @Value("${spring.http.connect.timeout:60000}")
    private int connectTimeout;

    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        return new RestTemplate(factory);
    }

    @Bean
    @ConditionalOnProperty(prefix = "spring.http.factory", value = {"type"},havingValue = "simple", matchIfMissing=true)
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        //默认的是JDK提供http连接，需要的话可以
        // 通过setRequestFactory方法替换为例如Apache HttpComponents、Netty或
        // OkHttp等其它HTTP library。
        log.info("http factory is simple");
        if (freeSsl){
            SSLClientHttpRequestFactory factory = new SSLClientHttpRequestFactory();
            factory.setReadTimeout(readTimeout);
            factory.setConnectTimeout(connectTimeout);
            return factory;
        } else {
            SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
            factory.setReadTimeout(readTimeout);
            factory.setConnectTimeout(connectTimeout);
            return factory;
        }



    }

    @Bean
    @ConditionalOnProperty(prefix = "spring.http.factory", value = {"type"},havingValue = "okhttp3", matchIfMissing=false)
    public ClientHttpRequestFactory okHttp3ClientHttpRequestFactory() {
        log.info("http factory is okhttp3");
        if (freeSsl){
            OkHttpClient okHttpClient = OkHttpFactory.getInstance();
            OkHttp3ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory(okHttpClient);
            return factory;
        } else {
            OkHttp3ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory();
            factory.setReadTimeout(readTimeout);
            factory.setConnectTimeout(connectTimeout);
            return factory;
        }
    }

    @Bean
    @ConditionalOnProperty(prefix = "spring.http.factory", value = {"type"},havingValue = "httpclient", matchIfMissing=false)
    public ClientHttpRequestFactory httpClientHttpRequestFactory() {
        log.info("http factory is httpclient");
        if (freeSsl){
            HttpClient httpClient = HttpClientFactory.getInstance();
            HttpComponentsClientHttpRequestFactory factory =
                    new HttpComponentsClientHttpRequestFactory(httpClient);
            return factory;
        } else {
            HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
            factory.setReadTimeout(readTimeout);
            factory.setConnectTimeout(connectTimeout);
            return factory;
        }
    }


}
